package com.interview.two.service;


import com.interview.two.AppConfig;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor;
import org.springframework.context.annotation.AnnotationConfigUtils;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

import java.io.FileNotFoundException;

@Slf4j
public class TestService3 {
    public static void main(String[] args) throws FileNotFoundException {
        GenericApplicationContext context = new GenericApplicationContext();
        AnnotationConfigUtils.registerAnnotationConfigProcessors(context.getDefaultListableBeanFactory());
        ConfigurationPropertiesBindingPostProcessor.register(context.getDefaultListableBeanFactory());
        context.registerBean(MyAspect.class);
        context.registerBean(AppConfig.class);
        context.refresh();

        Service3 bean = context.getBean(Service3.class);
        bean.transfer(1, 2, 500);
    }

    @Aspect
    // 处理上面说的（1-直接抛出异常，2-通过静态方法告诉事务通知，要回滚外），这里是因为优先级的关系导致，异常到了这个切面中
    // 切面中，却捕获了，并没有抛出，导致通知事务不知道
    //不指定顺序情况下： 通知事务-> 切面（不指定优先级的情况下）->目标方法（抛出异常）
    @Order(Ordered.LOWEST_PRECEDENCE - 1)
    static class MyAspect {
        @Around("execution(* transfer(..))")
        public Object around(ProceedingJoinPoint pjp) throws Throwable {
            log.error("执行了切面，啦啦啦 -------log:{}", pjp.getTarget());
            try {
                return pjp.proceed();
            } catch (Throwable e) {
                e.printStackTrace();
                return null;
            }
        }
    }
}
